package com.example.cuilei.kblight.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.example.cuilei.kblight.R;
import com.example.cuilei.kblight.shell.ShellBuilder;
import com.example.cuilei.kblight.util.LogUtils;
import com.example.cuilei.kblight.util.PreferencesUtil;
import com.example.cuilei.kblight.util.SwitchUtil;
import com.zcw.togglebutton.ToggleButton;

import java.util.Map;


public class MyActivity extends Activity {

    private Context context;
    private ToggleButton lightSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        lightSwitch = ((ToggleButton) findViewById(R.id.lightSwitch));
        ShellBuilder.execShell("");
        context = getApplicationContext();
        Map<String, String> ps = null;
        if ((ps = PreferencesUtil.get(getApplicationContext())) == null || ps.size() == 0) {
            lightSwitch.setToggleOn();
            LogUtils.info("没有读取到配置信息");
        } else {
            for (Map.Entry<String, String> m : ps.entrySet()) {
                String n = m.getKey();
                String v = m.getValue();
                if ("isOpen".equals(n)) {//判断键盘灯开关
                    if ("true".equals(v))
                        lightSwitch.setToggleOn();
                    else
                        lightSwitch.setToggleOff();
                }
            }
        }
        lightSwitch.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on)
                    SwitchUtil.open(context);
                else
                    SwitchUtil.close(context);
            }
        });
    }


}
